package org.redmagic.agent.utils;

import java.net.InetAddress;
import java.util.Properties;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.ThreadSafe;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Resolves pre-defined values using the local hostname.
 */
@ThreadSafe
@Immutable
public final class LocalHostProperties {

    private static final Log LOG = LogFactory.getLog(LocalHostProperties.class);
    
    public static final String HOSTNAME = "HOSTNAME";
    public static final String FQDN = "FQDN";
    public static final String IP_ADDRESS = "IP_ADDRESS";

    private static Properties properties = new Properties();

    static {
        try {
            InetAddress localhost = InetAddress.getLocalHost();

            properties.put(HOSTNAME, localhost.getHostName());
            properties.put(FQDN, localhost.getCanonicalHostName());
            properties.put(IP_ADDRESS, localhost.getHostAddress());

        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
    }

    private LocalHostProperties() {}
    
    @Nonnull
    public static Properties retrieve() {
        // return a defensive copy
        Properties defensiveCopy = new Properties();
        defensiveCopy.putAll(properties);
        return defensiveCopy;
    }
}